<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: tcpServer.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>tcpServer.c</h1><a href="tcp_server_8c.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* fpont 1/00 */</span>
<a name="l00002"></a>00002 <span class="comment">/* pont.net    */</span>
<a name="l00003"></a>00003 <span class="comment">/* tcpServer.c */</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;sys/socket.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;netinet/in.h&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;netdb.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;unistd.h&gt;</span> <span class="comment">/* close */</span>
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 
<a name="l00014"></a><a class="code" href="tcp_server_8c.html#a90cac659d18e8ef6294c7ae337f6b58">00014</a> <span class="preprocessor">#define SUCCESS 0</span>
<a name="l00015"></a><a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">00015</a> <span class="preprocessor"></span><span class="preprocessor">#define ERROR   1</span>
<a name="l00016"></a>00016 <span class="preprocessor"></span>
<a name="l00017"></a><a class="code" href="tcp_server_8c.html#6fd2b788cc016acd19820ddb30e78c1f">00017</a> <span class="preprocessor">#define END_LINE 0x0</span>
<a name="l00018"></a><a class="code" href="tcp_server_8c.html#c42367fe5c999ec6650de83e9d72fe8c">00018</a> <span class="preprocessor"></span><span class="preprocessor">#define SERVER_PORT 1500</span>
<a name="l00019"></a><a class="code" href="tcp_server_8c.html#a24597a54a085c6c2c33b64138f09eff">00019</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_MSG 100</span>
<a name="l00020"></a>00020 <span class="preprocessor"></span>
<a name="l00021"></a>00021 <span class="comment">/* function readline */</span>
<a name="l00022"></a>00022 <span class="keywordtype">int</span> <a class="code" href="tcp_server_8c.html#89682a2d7013984f72708bfce12e78b8">read_line</a>();
<a name="l00023"></a>00023 
<a name="l00024"></a><a class="code" href="tcp_server_8c.html#0ddf1224851353fc92bfbff6f499fa97">00024</a> <span class="keywordtype">int</span> <a class="code" href="test__hashtable_8c.html#3c04138a5bfe5d72780bb7e82a18e627">main</a> (<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) {
<a name="l00025"></a>00025   
<a name="l00026"></a>00026   <span class="keywordtype">int</span> sd, newSd, cliLen;
<a name="l00027"></a>00027 
<a name="l00028"></a>00028   <span class="keyword">struct </span>sockaddr_in cliAddr, servAddr;
<a name="l00029"></a>00029   <span class="keywordtype">char</span> line[<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>];
<a name="l00030"></a>00030 
<a name="l00031"></a>00031 
<a name="l00032"></a>00032   <span class="comment">/* create socket */</span>
<a name="l00033"></a>00033   sd = socket(AF_INET, SOCK_STREAM, 0);
<a name="l00034"></a>00034    <span class="keywordflow">if</span>(sd&lt;0) {
<a name="l00035"></a>00035     perror(<span class="stringliteral">"cannot open socket "</span>);
<a name="l00036"></a>00036     <span class="keywordflow">return</span> <a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">ERROR</a>;
<a name="l00037"></a>00037   }
<a name="l00038"></a>00038   
<a name="l00039"></a>00039   <span class="comment">/* bind server port */</span>
<a name="l00040"></a>00040   servAddr.sin_family = AF_INET;
<a name="l00041"></a>00041   servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
<a name="l00042"></a>00042   servAddr.sin_port = htons(<a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">SERVER_PORT</a>);
<a name="l00043"></a>00043   
<a name="l00044"></a>00044   <span class="keywordflow">if</span>(bind(sd, (<span class="keyword">struct</span> sockaddr *) &amp;servAddr, <span class="keyword">sizeof</span>(servAddr))&lt;0) {
<a name="l00045"></a>00045     perror(<span class="stringliteral">"cannot bind port "</span>);
<a name="l00046"></a>00046     <span class="keywordflow">return</span> <a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">ERROR</a>;
<a name="l00047"></a>00047   }
<a name="l00048"></a>00048 
<a name="l00049"></a>00049   listen(sd,5);
<a name="l00050"></a>00050   
<a name="l00051"></a>00051   <span class="keywordflow">while</span>(1) {
<a name="l00052"></a>00052 
<a name="l00053"></a>00053     printf(<span class="stringliteral">"%s: waiting for data on port TCP %u\n"</span>,argv[0],<a class="code" href="mcast_client_8c.html#c42367fe5c999ec6650de83e9d72fe8c">SERVER_PORT</a>);
<a name="l00054"></a>00054 
<a name="l00055"></a>00055     cliLen = <span class="keyword">sizeof</span>(cliAddr);
<a name="l00056"></a>00056     newSd = accept(sd, (<span class="keyword">struct</span> sockaddr *) &amp;cliAddr, &amp;cliLen);
<a name="l00057"></a>00057     <span class="keywordflow">if</span>(newSd&lt;0) {
<a name="l00058"></a>00058       perror(<span class="stringliteral">"cannot accept connection "</span>);
<a name="l00059"></a>00059       <span class="keywordflow">return</span> <a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">ERROR</a>;
<a name="l00060"></a>00060     }
<a name="l00061"></a>00061     
<a name="l00062"></a>00062     <span class="comment">/* init line */</span>
<a name="l00063"></a>00063     memset(line,0x0,<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>);
<a name="l00064"></a>00064     
<a name="l00065"></a>00065     <span class="comment">/* receive segments */</span>
<a name="l00066"></a>00066     <span class="keywordflow">while</span>(<a class="code" href="tcp_server_8c.html#89682a2d7013984f72708bfce12e78b8">read_line</a>(newSd,line)!=<a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">ERROR</a>) {
<a name="l00067"></a>00067       
<a name="l00068"></a>00068       printf(<span class="stringliteral">"%s: received from %s:TCP%d : %s\n"</span>, argv[0], 
<a name="l00069"></a>00069              inet_ntoa(cliAddr.sin_addr),
<a name="l00070"></a>00070              ntohs(cliAddr.sin_port), line);
<a name="l00071"></a>00071       <span class="comment">/* init line */</span>
<a name="l00072"></a>00072       memset(line,0x0,<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>);
<a name="l00073"></a>00073       
<a name="l00074"></a>00074     } <span class="comment">/* while(read_line) */</span>
<a name="l00075"></a>00075     
<a name="l00076"></a>00076   } <span class="comment">/* while (1) */</span>
<a name="l00077"></a>00077 
<a name="l00078"></a>00078 }
<a name="l00079"></a>00079 
<a name="l00080"></a>00080 
<a name="l00081"></a>00081 <span class="comment">/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING       */</span>
<a name="l00082"></a>00082 <span class="comment">/* this function is experimental.. I don't know yet if it works  */</span>
<a name="l00083"></a>00083 <span class="comment">/* correctly or not. Use Steven's readline() function to have    */</span>
<a name="l00084"></a>00084 <span class="comment">/* something robust.                                             */</span>
<a name="l00085"></a>00085 <span class="comment">/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING       */</span>
<a name="l00086"></a>00086 
<a name="l00087"></a>00087 <span class="comment">/* rcv_line is my function readline(). Data is read from the socket when */</span>
<a name="l00088"></a>00088 <span class="comment">/* needed, but not byte after bytes. All the received data is read.      */</span>
<a name="l00089"></a>00089 <span class="comment">/* This means only one call to recv(), instead of one call for           */</span>
<a name="l00090"></a>00090 <span class="comment">/* each received byte.                                                   */</span>
<a name="l00091"></a>00091 <span class="comment">/* You can set END_CHAR to whatever means endofline for you. (0x0A is \n)*/</span>
<a name="l00092"></a>00092 <span class="comment">/* read_lin returns the number of bytes returned in line_to_return       */</span>
<a name="l00093"></a><a class="code" href="tcp_server_8c.html#d0cbf67c79b321499f8348e6c1938d10">00093</a> <span class="keywordtype">int</span> <a class="code" href="tcp_server_8c.html#89682a2d7013984f72708bfce12e78b8">read_line</a>(<span class="keywordtype">int</span> newSd, <span class="keywordtype">char</span> *line_to_return) {
<a name="l00094"></a>00094   
<a name="l00095"></a>00095   <span class="keyword">static</span> <span class="keywordtype">int</span> rcv_ptr=0;
<a name="l00096"></a>00096   <span class="keyword">static</span> <span class="keywordtype">char</span> rcv_msg[<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>];
<a name="l00097"></a>00097   <span class="keyword">static</span> <span class="keywordtype">int</span> n;
<a name="l00098"></a>00098   <span class="keywordtype">int</span> offset;
<a name="l00099"></a>00099 
<a name="l00100"></a>00100   offset=0;
<a name="l00101"></a>00101 
<a name="l00102"></a>00102   <span class="keywordflow">while</span>(1) {
<a name="l00103"></a>00103     <span class="keywordflow">if</span>(rcv_ptr==0) {
<a name="l00104"></a>00104       <span class="comment">/* read data from socket */</span>
<a name="l00105"></a>00105       memset(rcv_msg,0x0,<a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>); <span class="comment">/* init buffer */</span>
<a name="l00106"></a>00106       n = recv(newSd, rcv_msg, <a class="code" href="mcast_client_8c.html#a24597a54a085c6c2c33b64138f09eff">MAX_MSG</a>, 0); <span class="comment">/* wait for data */</span>
<a name="l00107"></a>00107       <span class="keywordflow">if</span> (n&lt;0) {
<a name="l00108"></a>00108         perror(<span class="stringliteral">" cannot receive data "</span>);
<a name="l00109"></a>00109         <span class="keywordflow">return</span> <a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">ERROR</a>;
<a name="l00110"></a>00110       } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (n==0) {
<a name="l00111"></a>00111         printf(<span class="stringliteral">" connection closed by client\n"</span>);
<a name="l00112"></a>00112         close(newSd);
<a name="l00113"></a>00113         <span class="keywordflow">return</span> <a class="code" href="tcp_server_8c.html#8fe83ac76edc595f6b98cd4a4127aed5">ERROR</a>;
<a name="l00114"></a>00114       }
<a name="l00115"></a>00115     }
<a name="l00116"></a>00116   
<a name="l00117"></a>00117     <span class="comment">/* if new data read on socket */</span>
<a name="l00118"></a>00118     <span class="comment">/* OR */</span>
<a name="l00119"></a>00119     <span class="comment">/* if another line is still in buffer */</span>
<a name="l00120"></a>00120 
<a name="l00121"></a>00121     <span class="comment">/* copy line into 'line_to_return' */</span>
<a name="l00122"></a>00122     <span class="keywordflow">while</span>(*(rcv_msg+rcv_ptr)!=<a class="code" href="tcp_server_8c.html#6fd2b788cc016acd19820ddb30e78c1f">END_LINE</a> &amp;&amp; rcv_ptr&lt;n) {
<a name="l00123"></a>00123       memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1);
<a name="l00124"></a>00124       offset++;
<a name="l00125"></a>00125       rcv_ptr++;
<a name="l00126"></a>00126     }
<a name="l00127"></a>00127     
<a name="l00128"></a>00128     <span class="comment">/* end of line + end of buffer =&gt; return line */</span>
<a name="l00129"></a>00129     <span class="keywordflow">if</span>(rcv_ptr==n-1) { 
<a name="l00130"></a>00130       <span class="comment">/* set last byte to END_LINE */</span>
<a name="l00131"></a>00131       *(line_to_return+offset)=<a class="code" href="tcp_server_8c.html#6fd2b788cc016acd19820ddb30e78c1f">END_LINE</a>;
<a name="l00132"></a>00132       rcv_ptr=0;
<a name="l00133"></a>00133       <span class="keywordflow">return</span> ++offset;
<a name="l00134"></a>00134     } 
<a name="l00135"></a>00135     
<a name="l00136"></a>00136     <span class="comment">/* end of line but still some data in buffer =&gt; return line */</span>
<a name="l00137"></a>00137     <span class="keywordflow">if</span>(rcv_ptr &lt;n-1) {
<a name="l00138"></a>00138       <span class="comment">/* set last byte to END_LINE */</span>
<a name="l00139"></a>00139       *(line_to_return+offset)=<a class="code" href="tcp_server_8c.html#6fd2b788cc016acd19820ddb30e78c1f">END_LINE</a>;
<a name="l00140"></a>00140       rcv_ptr++;
<a name="l00141"></a>00141       <span class="keywordflow">return</span> ++offset;
<a name="l00142"></a>00142     }
<a name="l00143"></a>00143 
<a name="l00144"></a>00144     <span class="comment">/* end of buffer but line is not ended =&gt; */</span>
<a name="l00145"></a>00145     <span class="comment">/*  wait for more data to arrive on socket */</span>
<a name="l00146"></a>00146     <span class="keywordflow">if</span>(rcv_ptr == n) {
<a name="l00147"></a>00147       rcv_ptr = 0;
<a name="l00148"></a>00148     } 
<a name="l00149"></a>00149     
<a name="l00150"></a>00150   } <span class="comment">/* while */</span>
<a name="l00151"></a>00151 }
<a name="l00152"></a>00152   
<a name="l00153"></a>00153   
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 22:06:25 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
